package io.bufferedReader;

import java.io.*;

public class bufferedRead {
    public static void main(String[] args) throws Exception {
        bufferedRead bufferedRead = new bufferedRead();
        bufferedRead.copy1();
    }

    public void read() {
        String path = "d:/Test/create1.txt";
        BufferedReader bufferedReader = null;//操作字符的
        try {
            //字节输入流转换成字符输出流并指定编码
            InputStreamReader reader = new InputStreamReader(new FileInputStream(path), "GB2312");
            bufferedReader = new BufferedReader(reader);
            String line;
            bufferedReader.readLine();//按行读取，当读取到null读取完毕
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line + "\r\n");
            }
            System.out.println(stringBuilder.toString());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void write() throws IOException {
        String path = "d:/Test/create3.txt";
        BufferedWriter writer = new BufferedWriter(new FileWriter(path, true));
        writer.write("hello");
        writer.newLine();//插入一个与系统匹配的换行符
        writer.write("hello");
        writer.newLine();//插入一个与系统匹配的换行符
        writer.write("hello");
        writer.newLine();//插入一个与系统匹配的换行符
        writer.close();
    }

    public void copy() throws Exception {
        String source = "d:/Test/create1.txt";
        String target = "d:/Test/create2.txt";
        BufferedReader bufferedReader = new BufferedReader(new FileReader(source));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(target, true));
        String line;
        //bufferedReader.readLine() 读取一行,不包含换行符
        while ((line = bufferedReader.readLine()) != null) {
            bufferedWriter.write(line);
            bufferedWriter.newLine();
        }
        bufferedReader.close();
        bufferedWriter.close();
        System.out.println("拷贝完毕");
    }

    public void copy1() throws Exception {
        String source = "d:/Test/create1.txt";
        String target = "d:/Test/create3.txt";
        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(source));
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(source));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(target));
        byte[] bytes = new byte[1024];
        int readLean;
        while ((readLean = bufferedInputStream.read(bytes))!=-1){
            bufferedOutputStream.write(bytes,0,readLean);
        }
        bufferedOutputStream.close();
        bufferedInputStream.close();
    }
}


